package edu.pw.p2pclient.data;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import edu.pw.p2pclient.R;
import edu.pw.p2pclient.logic.SharedManager;

public class SharedTreeAdapter extends ArrayAdapter<Node> {

	private String mCurrentFolder = TreeNode.ROOT_PATH;
	private boolean mTree = false;
	private boolean mPush = false;
	private ViewHolder mViewHolder = null;

	public SharedTreeAdapter(Context context, boolean pushed) {

		super(context, R.layout.add_to_shared_row);
		mPush = pushed;
	}

	public String getParentFolder() {

		return mCurrentFolder.substring(0,
				Math.max(mCurrentFolder.lastIndexOf("/"), 1));
	}

	public String getCurrentFolder() {

		return mCurrentFolder;
	}

	public void refreshAdapter() {

		showDirectory(mCurrentFolder);
	}

	public void changeView() {

		mTree = !mTree;
		mCurrentFolder = TreeNode.ROOT_PATH;
		refreshAdapter();
	}

	public void showDirectory(String newPath) {

		mCurrentFolder = newPath;
		clear();
		ArrayList<Node> nodes = (mPush ? SharedManager.getInstance()
				.getPushShared() : SharedManager.getInstance()
				.getBtWifiShared());
		for (Node node : nodes) {
			if (mTree) {
				if (node.getPath().startsWith(mCurrentFolder)) {
					int pos = node.getFullPath()
							.substring(mCurrentFolder.length() + 1)
							.indexOf("/");
					if (pos == -1)
						add(new Node(node.getFullPath(), Node.FILE));
					else
						add(new Node(node.getPath().substring(0,
								pos + mCurrentFolder.length() + 1), Node.FOLDER));
				}
			} else
				add(node);
		}
		notifyDataSetChanged();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflater = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.shared_row, parent, false);

			mViewHolder = new ViewHolder();

			mViewHolder.name = (TextView) convertView
					.findViewById(R.id.sharedRowFilenameTextView);
			mViewHolder.path = (TextView) convertView
					.findViewById(R.id.sharedRowPathTextView);
			mViewHolder.image = (ImageView) convertView
					.findViewById(R.id.sharedRowImageView);
			mViewHolder.size = (TextView) convertView
					.findViewById(R.id.sharedRowSizeTextView);

			convertView.setTag(mViewHolder);
		} else {
			mViewHolder = (ViewHolder) convertView.getTag();
		}

		final Node node = getItem(position);

		if (mTree) {
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					if (!node.isFile())
						showDirectory(node.isMoveUp() ? getParentFolder()
								: node.getFullPath());
				}
			});
		} else
			convertView.setOnClickListener(null);

		mViewHolder.name.setText(node.getName());
		mViewHolder.path.setText(node.getFullPath().substring(0,
				node.getFullPath().lastIndexOf("/") + 1));
		if (node.isMoveUp())
			mViewHolder.image.setImageResource(R.drawable.move_waiting_up);
		else if (node.isFile()) {
			int res = 0;
			if (node.isShared() && node.isPush())
				res = R.drawable.file_shared_push;
			else if (node.isShared())
				res = R.drawable.file_shared;
			else if (node.isPush())
				res = R.drawable.file_push;
			else
				res = R.drawable.file;
			mViewHolder.image.setImageResource(res);
		} else {
			int res = 0;
			if (node.isShared() && node.isPush())
				res = R.drawable.folder_shared_push;
			else if (node.isShared())
				res = R.drawable.folder_shared;
			else if (node.isPush())
				res = R.drawable.folder_push;
			else
				res = R.drawable.folder;
			mViewHolder.image.setImageResource(res);
		}
		if (node.isFile()) {
			mViewHolder.size.setVisibility(View.VISIBLE);
			mViewHolder.size.setText(node.getSize());
		} else
			mViewHolder.size.setVisibility(View.INVISIBLE);

		return convertView;
	}

	private class ViewHolder {
		TextView name;
		TextView path;
		ImageView image;
		TextView size;
	}
}
